package com.atguigu;

import java.sql.Time;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName BlockingQueueDemo
 * @Author yy
 * @create 2022/9/8 20:04
 */
public class BlockingQueueDemo {
    public static void main(String[] args) {

        OverTime();
    }

    private static void OverTime() {
        BlockingQueue<Integer> blockingQueue = new ArrayBlockingQueue<>(3);

        new Thread(()->{
            try {
                blockingQueue.offer(1);
                blockingQueue.offer(2);
                blockingQueue.offer(3);
                System.out.println("--xxxx--");
//                System.out.println(blockingQueue.poll());
                blockingQueue.offer(4,3, TimeUnit.SECONDS);
//                blockingQueue.offer(5,3,TimeUnit.SECONDS);
            } catch (Exception e) {
                e.printStackTrace();
            }
        },"t1").start();
        /*
        当阻塞队列满时，
        队列会阻塞生产者线程一定时间，
        超过限时后生产者线程会退出
         */

        new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(2);
                System.out.println(blockingQueue.poll());
//                System.out.println(blockingQueue.remove());
//                System.out.println(blockingQueue.take());
                //take,remove移除都可以
                System.out.println(blockingQueue.poll());
                System.out.println(blockingQueue.poll());
                System.out.println(blockingQueue.poll());
                System.out.println(blockingQueue.poll(5,TimeUnit.SECONDS));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"t2").start();
    }

    private static void BlockQueue() {
        BlockingQueue<Integer> blockingQueue = new ArrayBlockingQueue<>(3);

        new Thread(()->{
            try {
                blockingQueue.put(1);
                blockingQueue.put(2);
                blockingQueue.put(3);
                System.out.println("-----aaa-----");
                //System.out.println(blockingQueue.take());
                blockingQueue.put(4);
                System.out.println("-----bbb-----");
                //System.out.println(blockingQueue.take());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"t1").start();

        /*
        当阻塞队列满时，
        生产者线程继续往队列里put元素，
        队列会一直阻塞生产者线程直到put数据or响应中断退出
         */

        new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(2);
                System.out.println(blockingQueue.take());
                System.out.println(blockingQueue.take());
                System.out.println(blockingQueue.take());
                System.out.println(blockingQueue.take());
                //System.out.println(blockingQueue.take());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"t2").start();

        /*
        当阻塞队列空时，
        消费者线程试图从队列里take元素，
        队列会一直阻塞消费者线程直到队列可用
         */
    }

    private static void TeShuZhi() {
        BlockingQueue<Integer> blockingQueue = new ArrayBlockingQueue<>(3);

        System.out.println(blockingQueue.offer(1));
        System.out.println(blockingQueue.offer(2));
        System.out.println(blockingQueue.offer(3));
        System.out.println(blockingQueue.offer(4));
        //插入方法，成功ture失败false

        System.out.println(blockingQueue.peek());
        //返回队列的头部元素,队列中没有元素时,返回null

        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.peek());
        //移除方法，成功返回出队列的元素，队列里没有就返回null
    }

    private static void ThrowsException() {
        BlockingQueue<Integer> blockingQueue = new ArrayBlockingQueue<>(3);

        //抛出异常
        blockingQueue.add(1);
        blockingQueue.add(2);
        blockingQueue.add(3);
//        blockingQueue.add(4);
        /*
        当阻塞队列满时，再往队列里add插入元素会抛
        IllegalStateException:Queue full
         */

//        System.out.println(blockingQueue.element());
        /*
        返回队列的头部元素
        当阻塞队列空时，再element也会抛
        NoSuchElementException
         */

        blockingQueue.remove();
        blockingQueue.remove();
        blockingQueue.remove();
//        blockingQueue.remove();

        /*
        当阻塞队列空时，再往队列里remove移除元素会抛
        NoSuchElementException
         */

//        System.out.println(blockingQueue.element());
    }
}
